package com.inter.insideinterservice.newservice;

import com.inter.entity.common.DeviceFlowUsage;
import com.inter.entity.newpojo.Constant;
import com.inter.entity.newpojo.TurntoInterfaceUtils;
import com.inter.entity.newpojo.request.RequestBody.QuerDeviceDayFlowRequestBody;
import com.inter.entity.newpojo.request.RequestHeader;
import com.inter.entity.newpojo.request.subRequest.QuerDeviceDayFlowRequest;
import com.inter.entity.newpojo.response.subResponse.QuerDeviceDayFlowResponse;
import com.inter.entity.newpojo.response.subResponse.common.FlowByDay;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class TurnToQuerDeviceDayFlowService {

    private static Logger logger = Logger.getLogger(TurnToQuerDeviceDayFlowService.class);

    public QuerDeviceDayFlowResponse queryDeviceDayFlow(String deviceId, String startDay, String endDay, String partnerCode, String secret) {

        try {

            QuerDeviceDayFlowRequestBody body = new QuerDeviceDayFlowRequestBody();
            body.setDeviceId(deviceId);
            body.setStartDay(startDay);
            body.setEndDay(endDay);

            // 请求参数拼接
            QuerDeviceDayFlowRequest request = new QuerDeviceDayFlowRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_singleDeviceDayFlow, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            //logger.info("签名是：" + sign);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QuerDeviceDayFlowRequest.class).toXml(request, "utf-8");
            //logger.info("请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //logger.info("返回结果：" + result);

            QuerDeviceDayFlowResponse responseObj = new JaxbUtil(QuerDeviceDayFlowResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                QuerDeviceDayFlowResponse response = new QuerDeviceDayFlowResponse();
                return response;

            } else {

                QuerDeviceDayFlowResponse response = new QuerDeviceDayFlowResponse();
                return response;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
